﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.Data.Xml.Dom;
using Windows.Storage;
using Windows.UI.Notifications;

namespace Tasks
{
    public sealed class DateConvertorBackgroundTask : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            UpdateEverything();
        }

        public static void UpdateEverything()
        {
            if (!ApplicationData.Current.LocalSettings.Values.Keys.Contains("LASTTIME"))
                ApplicationData.Current.LocalSettings.Values.Add("LASTTIME", (long)0);

            // Update current tile if required
            bool requiresUpdate = (long) ApplicationData.Current.LocalSettings.Values["LASTTIME"] != DateTime.Today.Ticks;
            //requiresUpdate = true;
            if (!requiresUpdate)
                return;
            
            ApplicationData.Current.LocalSettings.Values["LASTTIME"] = DateTime.Today.Ticks;

            DateTime today = DateTime.Today;
            DateTime tomorrow = today.AddDays(1);
            DateTime dayAfterTomorrow = tomorrow.AddDays(1);

            XmlDocument todayTile = GenerateTileForDate(today.Ticks);
            TileNotification myNotification = new TileNotification(todayTile);

            // Update current tile if required
            myNotification.ExpirationTime = tomorrow;
            TileUpdateManager.CreateTileUpdaterForApplication().Update(myNotification);

            // tomorrow planning
            XmlDocument tomXML = GenerateTileForDate(tomorrow.Ticks);
            ScheduledTileNotification tomTile = new ScheduledTileNotification(tomXML, tomorrow);
            tomTile.ExpirationTime = dayAfterTomorrow;
            TileUpdateManager.CreateTileUpdaterForApplication().AddToSchedule(tomTile);
        }

        private static XmlDocument GenerateTileForDate(long ticks)
        {
            DateTime dt = new DateTime(ticks);
            CalendarConversion cc = new CalendarConversion();
            cc.setGregorianDate(dt.Year, dt.Month, dt.Day);

            string txtDayNo = ConvertNumerals(cc.getIranianDay().ToString());
            string txtMount = cc.getMonthStrInPersian();
            string txtDayStr = cc.getWeekDayStrInPersian();

            // double tile
            XmlDocument newTile = new XmlDocument();
            newTile.LoadXml(
                @"<tile>
                    <visual>
                        <binding template=""TileSquareBlock"" branding=""name"">
                            <text id=""1""></text>
                            <text id=""2""></text>
                        </binding>  
                        <binding template=""TileWideBlockAndText01"" branding=""logo"">
                          <text id=""1""></text>
                          <text id=""2""></text>
                          <text id=""3""></text>
                          <text id=""4""></text>
                          <text id=""5""></text>
                          <text id=""6""></text>
                        </binding> 
                    </visual>
                </tile>");


            XmlNodeList xmlTexts = newTile.GetElementsByTagName("text");
            xmlTexts[0].AppendChild(newTile.CreateTextNode(txtDayNo));
            xmlTexts[1].AppendChild(newTile.CreateTextNode(txtMount));
            xmlTexts[2].AppendChild(newTile.CreateTextNode(txtDayStr));
            xmlTexts[6].AppendChild(newTile.CreateTextNode(txtDayNo));
            xmlTexts[7].AppendChild(newTile.CreateTextNode(txtMount));

            return newTile;
        }


        public static string ConvertNumerals(string input)
        {
            return input.Replace('0', '\u06f0')
                    .Replace('1', '\u06f1')
                    .Replace('2', '\u06f2')
                    .Replace('3', '\u06f3')
                    .Replace('4', '\u06f4')
                    .Replace('5', '\u06f5')
                    .Replace('6', '\u06f6')
                    .Replace('7', '\u06f7')
                    .Replace('8', '\u06f8')
                    .Replace('9', '\u06f9');
        }
    }
}
